package com.announcement.dao.Impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.announcement.dao.UserDao;
import com.announcement.entity.User;

@Component
public class UserDaoImpl extends BaseDaoImpl<User, Integer> implements UserDao {

	@Override
	public void test() {
		System.out.println("daoimpl");
	}

	@Override
	public User verifyUser(User user) {
		String email = user.getEmail();
		User result = this.uniqueResult("email", email);
		if (result != null) {
			if (user.getPassword().equals(result.getPassword())) {
				return result;
			}
		}
		return null;
	}

	@Override
	public boolean register(User user) {
		boolean status = false;
		int id = (int) this.save(user);
		if (id != 0) {
			status = true;
		}
		return status;
	}

	@Override
	public boolean updateUser(User user) {
		this.update(user);
		return true;
	}

	@Override
	public User findUserByName(String userName) {
		Criteria criteria = this.getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq("email", userName));
		return this.uniqueResult(criteria);
	}
}
